<template>
    <div class="mod-config">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <el-button v-if="isAuth('article:datatask:save')" type="primary" @click="addOrUpdateHandle()">新增
                </el-button>
                <el-button v-if="isAuth('article:datatask:delete')" type="danger" @click="deleteHandle()"
                           :disabled="dataListSelections.length <= 0">批量删除
                </el-button>
            </el-form-item>
        </el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%;">
            <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50">
            </el-table-column>
            <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="名称">
            </el-table-column>
            <el-table-column
                prop="type"
                header-align="center"
                align="center"
                label="类型">
            </el-table-column>
            <el-table-column
                prop="catalogueUrl"
                header-align="center"
                align="center"
                label="目录url">
            </el-table-column>
            <el-table-column
                prop="catalogueRule"
                header-align="center"
                align="center"
                label="目录规则">
            </el-table-column>
            <el-table-column
                prop="contentRule"
                header-align="center"
                align="center"
                label="内容规则">
            </el-table-column>
            <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                width="150"
                label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="exec(scope.row.id)"
                               v-loading="tableLoading && tableLoadingId === scope.row.id">执行
                    </el-button>
                    <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
                    <div v-if="percentage != 0">
                        <el-progress :percentage="percentage" :color="percentageChange"></el-progress>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</template>

<script>
    import AddOrUpdate from './datatask-add-or-update';

    export default {
        data() {
            return {
                dataForm: {
                    key: ''
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false,

                tableLoading: false, //全屏锁定
                tableLoadingId: '',
                percentage: 0 //进度展示
            };
        },
        components: {
            AddOrUpdate
        },
        activated() {
            this.getDataList();
        },
        methods: {
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true;
                this.$api.DataTask.list({
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'key': this.dataForm.key
                }).then((data) => {
                    if (data && data.code === 0) {
                        this.dataList = data.page.list.map(item => {
                            let temp = item.type == 0 ? '新闻' : '小说';
                            item.type = temp;
                            return item;
                        });
                        this.totalPage = data.page.totalCount;
                    } else {
                        this.dataList = [];
                        this.totalPage = 0;
                    }
                    this.dataListLoading = false;
                });
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val;
                this.pageIndex = 1;
                this.getDataList();
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val;
                this.getDataList();
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val;
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true;
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id);
                });
            },
            //执行
            exec(id) {
                this.tableLoading = true;
                this.tableLoadingId = id;
                setTimeout(() => {
                    this.$api.DataTask.exec(id)
                        .then(data => {
                            if (data && data.code === 0) {
                                this.tableLoading = false;
                                this.tableLoadingId = '';
                                this.$message.success(data.msg+"开始抓取....");

                                let _self = this;
                                _self.percentage = 0;
                                let timer = setInterval(()=>{
                                    this.$api.DataTask.execPercentage(id)
                                        .then(data => {
                                            _self.percentage = data.percentage;
                                        });
                                    if(_self.percentage === 100){
                                        clearInterval(timer);
                                    }
                                },1000);

                            }
                        }).catch(data => {
                        this.tableLoading = false;
                        this.tableLoadingId = '';
                        this.$message.info(data.msg);
                    });
                }, 3000);
            },
            // 删除
            deleteHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.id;
                });
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$api.DataTask.del(ids).then((data) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList();
                                }
                            });
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                });
            },

            percentageChange() {
                if (this.percentage < 30) {
                    return '#909399';
                } else if (this.percentage < 70) {
                    return '#e6a23c';
                } else {
                    return '#67c23a';
                }
            }
        }
    };
</script>
